package com.ming.service.impl;

import com.ming.mapper.AccessRecordMapper;
import com.ming.mapper.CarMapper;
import com.ming.pojo.AccessRecord;
import com.ming.service.AccessRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AccessRecordServiceImpl implements AccessRecordService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private AccessRecordMapper recordMapper;

    @Override
    public Boolean addEnter(String carLicenseId) {

        //获取当前(进入)时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        java.util.Date date = new Date();//获得当前时间
        String ts= df.format(date);//将当前时间转换成特定格式的时间字符串，这样便可以插入到数据库中

        //判断是否为业主车
        Boolean isInterior;
        if(carMapper.queryOwnerByCarLicense(carLicenseId) == null){
            isInterior = false;
        } else {
            isInterior = true;
        }

        recordMapper.addEnterRecord(carLicenseId, ts, isInterior);

        return isInterior;
    }

    @Override
    public AccessRecord addLeave(String carLicenseId){

        //System.out.println(carLicenseId);

        //获取enter(进入)时间
        Date enterDate = recordMapper.queryEnterByCarId(carLicenseId);

        //获取当前(离开)时间
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月和小时的格式为两个大写字母
        Date leaveDate = new Date();//获得当前时间
        String leaveTime= df.format(leaveDate);//将当前时间转换成特定格式的时间字符串，这样便可以插入到数据库中
        //System.out.println("###########################");
        //System.out.println(leaveDate.getTime());
        //System.out.println(leaveDate);
        //System.out.println(enterDate.getTime());
        //System.out.println(enterDate);

        //计算收费
        BigDecimal expense = new BigDecimal("0");
        if(carMapper.queryOwnerByCarLicense(carLicenseId) == null) {
            long ms = leaveDate.getTime() - enterDate.getTime();
            //小于半小时不收费
            //System.out.println(ms);
            //System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%");
            if((ms/3600000) >= 0.5) {
                //BigDecimal expense = new BigDecimal("0");
                int h = (int)(ms/3600000);

                double cost = 3.00;
                String expenseString = String.valueOf(cost * (h+1));

                BigDecimal expense2 = new BigDecimal(expenseString);
                //System.out.println(expense2);
                //System.out.println("---------------------------");
                expense = expense.add(expense2);
            }
        }

        //System.out.println(expense);

        recordMapper.addLeaveRecord(carLicenseId,leaveTime,expense);

        //判断是否为业主车
        Boolean isInterior;
        if(carMapper.queryOwnerByCarLicense(carLicenseId) == null){
            isInterior = false;
        } else {
            isInterior = true;
        }

        AccessRecord accessRecord = new AccessRecord(null,null,null,isInterior,expense);

        return accessRecord;
        //System.out.println(enterDate);
        //System.out.println(leaveTime);
        //System.out.println(ms/600000);

    }

    @Override
    public List<AccessRecord> allList() {

        return recordMapper.queryAllRecord();
    }

    @Override
    public List<AccessRecord> listByCarId(String carLicenseId) {

        carLicenseId = '%' + carLicenseId + '%';
        return recordMapper.queryRecordByCarId(carLicenseId);
    }

    @Override
    public List<AccessRecord> listByInterior(boolean isInterior) {

        return recordMapper.queryRecordByIs(isInterior);
    }

    @Override
    public void addRecord(AccessRecord accessRecord) {
        recordMapper.addRecord(accessRecord);
    }


    @Override
    public void deleteRecord(AccessRecord accessRecord) {
        recordMapper.deleteRecord(accessRecord.getCarLicenseId(),accessRecord.getEnterTime());
    }


}
